package com.store.service.impl;

import com.store.entity.User;
import com.store.mapper.UserMapper;
import com.store.service.UserService;
import com.store.service.ex.*;
import com.store.util.MD5Util;
import com.store.util.PageHelper;
import com.store.util.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 业务层（用户模块）
 */
@DubboService(interfaceClass = UserService.class,version = "1.0.0",timeout = 5000)
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 注册
     * @param user 用户数据
     */
    @Override
    public void addUser(User user) {
        User exists = userMapper.selectUserByUsername(user.getUsername());
        if(exists != null){
            throw new UserExistsException("用户已经存在");
        }else{
            //明文加密
            String salt = UUID.randomUUID().toString().toUpperCase();
            String newPassword = MD5Util.setMD5Password(user.getPassword(),salt);
            //封装数据
            user.setPassword(newPassword);
            user.setSalt(salt);
            user.setIsDelete(0);
            user.setCreatedUser(user.getUsername());
            user.setCreatedTime(new Date());
            user.setModifiedUser(user.getUsername());
            user.setModifiedTime(new Date());
            int rows = userMapper.addUser(user);
            if(rows != 1){
                throw new InsertException("注册时产生未知的异常");
            }
        }
    }

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @Override
    public User getUserByUsername(String username, String password) {
        User exists = userMapper.selectUserByUsername(username);
        if(exists == null){
            throw new UserNotFoundException("用户不存在");
        }else {
            String newPassword = MD5Util.setMD5Password(password,exists.getSalt());
            String oldPassword = exists.getPassword();
            if(!newPassword.equals(oldPassword)){
                throw new PasswordErrorException("用户密码错误");
            }else {
                //过滤数据
                User user = new User();
                user.setUsername(exists.getUsername());
                user.setUid(exists.getUid());
                user.setAvatar(exists.getAvatar());
                return user;
            }
        }
    }

    /**
     * 删除用户
     * @param uid 用户id
     */
    @Override
    public void deleteUserById(Integer uid) {
        User exists = userMapper.selectUserByUid(uid);
        if(exists == null){
            throw new UserNotFoundException("用户不存在");
        }else {
            int rows = userMapper.deleteUserByUid(uid);
            if(rows != 1){
                throw new DeleteException("删除用户时出现未知的异常");
            }
        }
    }

    /**
     * 分页查询用户
     * @return
     */
    @Override
    public PageInfo<User> getAllUser(Integer current,Integer size) {
        List<User> userList = userMapper.queryAllUser();
        PageInfo<User> pageInfo;
        if(userList == null){
            throw new UserNotFoundException("用户数据不存在");
        }else {
            PageHelper<User> pageHelper = new PageHelper<>(size,current);
            pageInfo = pageHelper.getPageInfo(userList);
        }
        return pageInfo;
    }

    /**
     * 修改密码
     * @param uid 用户id
     * @param oldPassword 新密码
     */
    @Override
    public void updateUserPassword(Integer uid, String oldPassword) {
        User exists = userMapper.selectUserByUid(uid);
        if(exists == null){
            throw new UserNotFoundException("目标用户不存在!");
        }
        //明文加密
        String salt = UUID.randomUUID().toString().toUpperCase();
        String password = MD5Util.setMD5Password(oldPassword,salt);
        int rows = userMapper.updateUserPassword(uid, password, salt);
        if(rows != 1){
            throw new UpdateException("更新密码产生未知的异常!");
        }
    }

    @Override
    public String getEmail(String username) {
        User exists = userMapper.selectUserByUsername(username);
        if(exists == null){
            throw new UserNotFoundException("目标用户不存在");
        }
        return exists.getEmail();
    }
}
